package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private ISetmealService setmealService;

    @CacheEvict(value = "setMealCache", allEntries = true)
    @PostMapping
    public R<String> saveMeal(@RequestBody SetmealDto setmealDto, HttpServletRequest request){
        setmealService.saveMeal(setmealDto,request);
        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R<Page> selectPage(Integer page, Integer pageSize, String name){
        Page<SetmealDto> setmealDtoPage = setmealService.selectPage(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    @CacheEvict(value = "setMealCache", allEntries = true)
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids, Integer status){
        setmealService.deleteByIds(ids, status);
        return R.success("套餐删除成功..");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
        setmealService.updateStatus(status, ids);
        return R.success("操作成功..");
    }

    @Cacheable(value = "setMealCache", key = "#id")
    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.selectById(id);
        return R.success(setmealDto);
    }

    @CacheEvict(value = "setMealCache", allEntries = true)
    @PutMapping
    public R<String> updateInfo(@RequestBody SetmealDto setmealDto, HttpServletRequest request){
        setmealService.updateInfo(setmealDto, request);
        return R.success("修改套餐成功..");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setMealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
